﻿using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using HiCore.Extensions.DependencyInjection;
using HiCore.WebApi.Example.IRepository;

namespace HiCore.WebApi.Producer.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {
        private readonly ILogger<HomeController> _logger;
        private readonly ITestRepository _testRepository;
        public HomeController(ILogger<HomeController> logger, ITestRepository testRepository)
        {
            _logger = logger;
            _testRepository = testRepository;
        }

        [HttpGet("add")]
        //[NoApiSign]
        public async Task<int> Add()
        {
            return await _testRepository.AddAsync();
        }

        [HttpGet("update")]
        public async Task<int> Update(int id)
        {
            return await _testRepository.UpdateAsync(id);
        }

        [HttpGet("remove")]
        public async Task<int> Remove(int id)
        {
            return await _testRepository.RemoveAsync(id);
        }

        [HttpGet("get")]
        public async Task<object> Get(int id)
        {
            return new
            {
                Data = await _testRepository.GetAsync(id)
            };
        }

        /// <summary>
        /// 执行原生SQL
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("execute")]
        public async Task<int> Execute(int id)
        {
            return await _testRepository.ExecuteAsync(id);
        }

        [HttpGet("transaction")]
        public async Task<int> Transaction(int id)
        {
            await _testRepository.Transaction(id);
            return await Task.FromResult(0);
        }

        /// <summary>
        /// 带条件修改无法加密(执行原生SQL)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("updaterange")]
        public async Task<int> UpdateRange(int id)
        {
            return await _testRepository.UpdateRangeAsync(id);
        }
    }

    public class AA
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
